<template>
    <div class="home">
        <!-- 导航头部 -->
        <div class="search">
            <van-nav-bar>
                <template #left>
                    <span class="city" @click="toCity">{{nowPosition}}</span>
                    <van-icon name="search" size="18" class="home-nav-search-icon" />
                    <van-field
                    v-model="value" placeholder="请输入用户名" class="home-nav-search" @focus="toSearch" />
                </template>
            </van-nav-bar>
        </div>
        <!-- banner -->
        <div class="swiper">
            <van-swipe style="margin-top: 1.226667rem" indicator-color="rgb(0, 0, 0)">
                <van-swipe-item v-for="(item, index) in homeData.banner" :key="index">
                    <img :src="item.image_url" class="home-banner-img"/>
                </van-swipe-item>
            </van-swipe>
        </div>
        <!-- 次级导航 -->
        <div class="channel">
            <van-grid :column-num="5" square>
                <van-grid-item
                    v-for="(item, index) in homeData.channel"
                    :key="index"
                    :to="{ path: '/homeCategory', query: { id: item.id } }"
                >
                    <van-image :src="item.icon_url" class="home-nav2-img" />
                    <p>{{item.name}}</p>
                </van-grid-item>
            </van-grid>
            <div class="bgc-8px"></div>
        </div>
        <!-- 品牌制造商直供 -->
        <div class="brand">
            <!-- 头部 -->
            <div class="header" @click="toBrandlist">
                <van-nav-bar>
                    <template #title>
                        <span class="brand-title">品牌制造商直供</span>
                    </template>
                </van-nav-bar>
            </div>
            <!-- 内容 -->
            <div class="brand-content">
                <div
                    v-for="(item, index) in homeData.brandList"
                    :key="index"
                    class="brand-item"
                    :style="'background: url(' + item.new_pic_url + ')'"
                    @click="toBrandDetail(item.id)"
                >
                    <p>{{item.name}}</p>
                    <p>{{item.floor_price + '起'}}</p>
                </div>
            </div>
        </div>
        <!-- 新品首发 -->
        <div class="newgoods">
            <!-- 头部 -->
            <div class="top">
                <!-- @/assets/img/bgnew.png -->
                <div class="wrapper" @click="toNewGoods(1)">
                    <div class="wrapper-center">
                        <p class="title">新品首发</p>
                        <p class="checkAll">查看全部</p>
                    </div>
                </div>
            </div>
            <!-- 详情 -->
            <ul class="items">
                <li
                    class="item"
                    v-for="(item, index) in homeData.newGoods"
                    :key="index"
                    @click="toGoods(item.id)"
                >
                    <img :src="item.list_pic_url" alt="">
                    <p class="title">{{item.name}}</p>
                    <p class="info">{{item.goods_brief}}</p>
                    <p class="price">￥{{item.retail_price}}</p>
                </li>
            </ul>
        </div>
        <!-- 人气推荐 -->
        <div class="hotgoods">
            <!-- 头部 -->
            <div class="top">
                <!-- @/assets/img/bgnew.png -->
                <div class="wrapper" @click="toNewGoods()">
                    <div class="wrapper-center">
                        <p class="title">人气推荐·好物精选</p>
                        <p class="checkAll">查看全部</p>
                    </div>
                </div>
            </div>
            <!-- 详情 -->
            <ul class="items">
                <li
                    class="item"
                    v-for="(item, index) in homeData.hotGoods"
                    :key="index"
                    @click="toGoods(item.id)"
                >
                    <img :src="item.list_pic_url" alt="">
                    <p class="title">{{item.name}}</p>
                    <p class="info">{{item.goods_brief}}</p>
                    <p class="price">￥{{item.retail_price}}</p>
                </li>
            </ul>
        </div>
        <!-- 专题精选 -->
        <div class="topicList">
            <div class="bgc-8px"></div>
            <!-- 头部 -->
            <van-nav-bar>
                <template #title>
                    <div class="title-wrapper">
                        <div class="brand-title">专题精选</div>
                        <div class="title-icon"></div>
                    </div>
                </template>
            </van-nav-bar>
            <!-- 详情 -->
            <ul class="items">
                <li
                    class="item"
                    v-for="(item, index) in homeData.topicList"
                    :key="index"
                    @click="toTopicDetail(item.id)"
                >
                    <img :src="item.item_pic_url" alt="">
                    <div class="desc">
                        <div class="title-wrapper">
                            <p class="title">{{item.title}}</p>
                            <p class="info">{{item.subtitle}}</p>
                        </div>
                        <p class="price">{{item.price_info}}元起</p>
                    </div>
                </li>
            </ul>
        </div>
        <!-- 好物 -->
        <div class="newcategory">
            <div class="list" v-for="(item, index) in homeData.newCategoryList" :key="index">
                <!-- 头部 -->
                <van-nav-bar class="header">
                    <template #title>
                        <span class="brand-title">{{item.name}}好物</span>
                    </template>
                </van-nav-bar>
                <ul class="list-items">
                    <div
                        class="list-item"
                        v-for="(val, id) in item.goodsList"
                        :key="id"
                        @click="toGoods(val.id)"
                    >
                        <img :src="val.list_pic_url" alt="">
                        <p>{{val.name}}</p>
                        <p>￥{{val.retail_price}}</p>
                    </div>
                    <div
                        class="list-item moreGoods"
                        @click="toHomeCategory(item.id)"
                    >
                        <div class="moreGoods-wrapper">
                            <div>{{item.name}}好物</div>
                            <img src="../../assets/img/icon_go_more.png" alt="">
                        </div>
                    </div>
                </ul>
            </div>
        </div>
    </div>
</template>

<script>
import {getHomeInfo} from '@/api/home/index'
import { mapState } from 'vuex'

export default {
    data() {
        return {
            value: '',
            homeData: ''
        }
    },
    computed: {
        ...mapState({
            nowPosition: state => state.nowPosition
        })
    },
    methods: {
        // 跳转到城市选择
        toCity() {
            this.$router.push({ name: 'city'})
        },
        //点击跳转到搜索页面
        toSearch() {
            this.$router.push({ name: 'search'})
        },
        // 点击跳转品牌制造商直供
        toBrandlist() {
            this.$router.push({ name: 'brandlist', query: { page: 1 } })
        },
        // 点击跳转具体制造商
        toBrandDetail(id) {
            this.$router.push({ name: 'brandDetail', query: { id, } })
        },
        // 跳转新品首发或者好物精选
        toNewGoods(flag) {
            if (flag) {
                this.$router.push({
                    name: 'newGoods',
                    query: {
                        isHot: '',
                        isNew: 1,
                    }
                })
            } else {
                this.$router.push({
                    name: 'newGoods',
                    query: {
                        isHot: 1,
                        isNew: '',
                    }
                })
            }
        },
        // 跳转到专题详情
        toTopicDetail(id) {
            this.$router.push({ name: 'topicDetail', query: {id,} })
        },
        // 商品详情跳转
        toGoods(id) {
            this.$router.push( { path: '/pages/goods', query: { id, } } )
        },
        // 跳转到商品分类
        toHomeCategory(id) {
            this.$router.push({ name: 'homeCategory', query: { id, } })
        }
    },
    created() {
        var _this = this;
        // 本地存储个人信息
         var userInfo = {
            "openId":"13040287368",
            "nickName":"刘定康",
            "avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIbWFEIJj8IpGeHM7dGic1aTFZALjWcMm9ltWfFiaQfVRYticWBfgGfzXWMt2EkJWiaicPtftHAlWxUibxQ/132"
        };
        var openId = userInfo.openId;
        localStorage.setItem("userInfo", JSON.stringify(userInfo));
        localStorage.setItem("openId", openId);
        // 获取首页数据
        getHomeInfo()
        .then( res => {
            _this.homeData = res
        } )
    }
}
</script>

<style lang="scss">
    @mixin newgoods {
        width: 100%;
        .top{
            width: 100%;
            height: 150px;
            background: #f4f4f4;
            display: flex;
            align-items: center;
            .wrapper{
                width: 100%;
                height: 130px;
                background: url(../../assets/img/bgnew.png) no-repeat;
                background-size: 100%;
                display: flex;
                .wrapper-center{
                    margin: auto;
                    text-align: center;
                    color: #8c9bae;
                    .title{
                        font-size: .42667rem
                    }
                    .checkAll{
                        width: 2.4rem;
                        height: .66667rem;
                        line-height: .66667rem;
                        margin: .36rem auto 0 auto;
                        font-size: .29333rem;
                        background: #d8e4f0;
                    }
                }
            }
        }
        .items{
            display: flex;
            overflow: auto;
            padding-bottom: 10px;
            margin-bottom: 10px;
            .item:nth-child(-n+3) {
                border-right: solid #f4f4f4 2px;
            }
            .item{
                width: 3.73333rem;
                // height: 5.54667rem;
                margin: .06667rem 0 .06667rem .33333rem;
                flex-shrink: 0;
                img{
                    display: block;
                    width: 100%;
                    height: 3.73333rem;
                }
                p{
                    width: 94%;
                    margin-top: .10667rem;
                    text-indent: 1em;
                    overflow: hidden;
                    white-space: nowrap;
                    text-overflow: ellipsis;
                }
                .title{
                    font-size: .4rem;
                    font-weight: 700;
                }
                .info{
                    color: #8a8a8a;
                    font-size: .32rem;
                }
                .price{
                    color: #9c3232;
                    font-size: .32rem;
                }
            }
        }
    }
    .home{
        width: 100%;
        .search{
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            width: 100%;
            background: #fff;
            z-index: 9;
            .city{
                display: block;
                width: 50px;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
            }
            .home-nav-search-icon{
                margin-left: 25px;
                margin-top: 2px
            }
            .home-nav-search{
                width: 60%;
            }
        }
        .swiper{
            width: 100%;
            .home-banner-img{
                width: 100%;
                height: 100%;
            }
            .van-swipe__indicators i{
                background: rgb(51, 51, 51);
            }
        }
        .channel{
            width: 100%;
            .home-nav2-img{
                width: 29px;
                height: 29px;
                margin-top: 10px;
            }
        }
        .brand{
            width: 100%;
            .brand-title{
                font-size: 12px;
            }
            .brand-content{
                width: 100%;
                height: 244px;
                display: flex;
                flex-wrap: wrap;
                justify-content: space-evenly;
                .brand-item{
                    margin-bottom: 6px;
                    background-size: 100% !important;
                    width: 178px;
                    height: 116px;
                    p{
                        padding-left: 8px;
                        padding-top: 5px;
                        font-size: 12px;
                    }
                }
            }
        }
        .newgoods{
            @include newgoods;
        }
        .hotgoods{
            @include newgoods;
            .top{
                .wrapper{
                    background: url(../../assets/img/bgtopic.png) no-repeat;
                }
            }
            .items{
                .item:nth-child(-n+4) {
                    border-right: solid #f4f4f4 2px;
                }
            }
        }
        .topicList{
            .title-wrapper{
                width: 80px;
                text-align: center;
                margin: 0 auto;
                display: flex;
                .brand-title{
                    font-size: 12px;
                }
                .title-icon{
                    width: 16px;
                    height: 16px;
                    margin-left: 5px;
                    background: url(../../assets/img/right.png);
                    background-size: 100%;
                }
            }
            .items{
                display: flex;
                overflow: auto;
                padding-bottom: 10px;
                margin-bottom: 10px;
                .item{
                    width: 7.66667rem;
                    margin-left: .33333rem;
                    flex-shrink: 0;
                    img{
                        display: block;
                        width: 100%;
                        height: 162.5px;
                        border-radius: 8px;
                    }
                    .desc{
                        display: flex;
                        .title-wrapper{
                            width: 80%;
                            display: flex;
                            flex-wrap: wrap;
                            p{
                                width: 100%;
                                flex-shrink: 0;
                                text-align: left;
                            }
                            .title{
                                font-size: .4rem;
                                font-weight: 700;
                            }
                            .info{
                                color: #8a8a8a;
                                font-size: .32rem;
                            }
                        }
                        .price{
                            color: #9c3232;
                            font-size: .32rem;
                        }
                        p{
                            margin-top: .10667rem;
                            overflow: hidden;
                            white-space: nowrap;
                            text-overflow: ellipsis;
                        }
                    }



                }
            }
        }
        .newcategory{
            width: 100%;
            .list{
                width: 100%;
                background: #f4f4f4;
                .header{
                    background: #f4f4f4;
                    .brand-title{
                        font-size: 12px;
                    }
                }
                .list-items{
                    display: flex;
                    flex-wrap: wrap;
                    justify-content: space-evenly;
                    .list-item{
                        display: flex;
                        flex-wrap: wrap;
                        justify-content: center;
                        margin-bottom: 6px;
                        width: 180px;
                        background: #fff;
                        img{
                            width: 151px;
                            height: 151px;
                        }
                        p{
                            width: 180px;
                            margin: 0 0 12px 20px;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                        }
                        p:last-child {
                            color: #9c3232;
                        }
                    }
                    .moreGoods{
                        display: flex;
                        .moreGoods-wrapper{
                            width: 80px;
                            margin: auto;
                            text-align: center;
                            div{
                                font-size: 17px;
                                margin-bottom: 10px;
                            }
                            img{
                                width: 40px;
                                height: 40px;
                            }
                        }
                    }
                }
            }
        }
        .bgc-8px{
            height: 8px;
            background: #f4f4f4;
        }
    }

</style>
